import VueRouter from 'vue-router'
import Vue from 'vue'
import LoginIndex from '../views/login/login.vue'
import auth from '@/utils/auth'
import Layout from '@/views/Layout'
import Home from '@/views/home/home'
import Article from '@/views/article'
import Publish from '@/views/publish/publish.vue'

Vue.use(VueRouter)
const router = new VueRouter({
  routes: [{ path: '/login', component: LoginIndex },
    {
      path: '/',
      component: Layout,
      children: [
        { path: '/', component: Home }, { path: '/article', component: Article },
        { path: '/publish', component: Publish }
      ]
    }
  ] // 路由规则
})
export default router

router.beforeEach((to, from, next) => {
  // 获取token
  const token = auth.getToken()
  // 不是访问登录，有没有token，跳转登录页面
  if (to.path !== '/login' && !token) return next('/login')
  // 其他情况放行
  next()
})
